<template>
    <div class="post-item">
        <div class="post-type center-flex-box">
            <el-tag :type="type.type" class="type-tag">{{type.text}}</el-tag>
        </div>
        <div class="post-box center-flex-box">
            <div class="outer">
                <div class="forward">
                    <a class="post-title" @click="goToPost">{{post.title}}</a>
                </div>
                <a class="post-content">{{post.content}}</a>
            </div>
        </div>
        <div class="post-info-box center-flex-box">
            <div class="outer">
            <span class="label">
                <div v-if="post.labels">
                    <div class="center-flex-box" v-for="label in post.labels.slice(0,3)" :key="label.id">
                    <label-tag class="tag"
                               :label="label"
                               :link="link"
                               @changeLabelAttention="changeLabelAttention"></label-tag>
                </div>
                </div>
            </span>
                <span class="last-reply-time">{{lastReplyTime}}</span>
            </div>
        </div>
        <div class="post-count center-flex-box">
            <div class="outer">
                <span class="collect">收藏数：{{post.collectCount}}</span>
                <span class="reply">评论数：{{post.replyCount}}</span>
            </div>
        </div>
        <simple-user-info-right class="simple-user-info" :user-info="post.author"></simple-user-info-right>
    </div>
</template>

<script>
    import SimpleUserInfoRight from '@/components/user/SimpleUserInfoRight'
    import LabelTag from '@/components/label/LabelTag'
    import { convertLTime } from '@/utils/date'
    import { postType } from '@/constant'

    export default {
        name: 'postItem',
        components: {
            SimpleUserInfoRight,
            LabelTag
        },
        props: {
            post: {
                type: Object,
                required: false
            },
            link: {
                type: Boolean,
                required: false,
                default: false
            }
        },
        computed: {
            type () {
                return postType[this.post.type]
            },
            lastReplyTime () {
                return convertLTime(this.post.lastReplyTime)
            }
        },
        methods: {
            goToPost () {
                this.$router.push(`/area/${this.post.area}/${this.post.id}`)
            },
            changeLabelAttention (data) {
                if (this.link) {
                    this.$emit('changeLabelAttention', data)
                }
            }
        }
    }
</script>

<style lang="scss" scoped>
    .center-flex-box {
        vertical-align: top;
        display: -webkit-inline-flex;
        justify-content: center;
        align-items: center;
    }

    .outer {
        display: inline-block;
        text-align: left;
        width: 100%;
    }

    .post-item {
        height: 80px;
        margin: 5px 0;

        .post-type {
            width: 8%;
            height: 100%;
            margin: 0 1%;

            .type-tag {
                width: 120px;
            }
        }

        .post-box {
            width: 45%;
            height: 100%;

            .forward {
                .post-title {
                    font-size: 16px;
                    font-weight: bolder;

                    overflow: hidden;
                    text-overflow: ellipsis;
                    display: -webkit-box;
                    -webkit-box-orient: vertical;
                    -webkit-line-clamp: 1;
                }

                a {
                    text-decoration: none;
                    cursor: pointer;
                }

                a:link {
                    color: #216278;
                }

                a:visited {
                    color: #216278;
                }

                a:hover {
                    color: rgba(255, 151, 60, 0.8);
                }

                a:active {
                    color: rgba(255, 86, 25, 0.8);
                }
            }

            .post-content {
                font-size: 14px;

                overflow: hidden;
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 1;
            }
        }

        .post-info-box {
            height: 100%;
            width: 20%;

            .label {
                display: -webkit-inline-flex;
                justify-content: center;
                float: top;
                height: 60%;
                width: 100%;

                .tag {
                    margin: 2px;
                }
            }

            .last-reply-time {
                display: inline-block;
                font-size: 12px;
                width: 100%;
                text-align: center;
            }
        }

        .post-count {
            height: 100%;
            width: 10%;

            span {
                display: inline-block;
                font-size: 12px;
                line-height: 25px;
                width: 100%;
                text-align: center;
            }

            .collect {
                color: rgba(255, 103, 51, 0.8);
            }

            .reply {
                color: rgba(102, 69, 255, 0.8);
            }
        }

        .simple-user-info {
            vertical-align: top;
            display: -webkit-inline-flex;
            justify-content: center;
            align-items: center;
            height: 100%;
            width: 15%;
        }
    }
</style>
